#!/bin/bash

# run & log functions
. /etc/rc.d/rc.runlog

queue(){
  atq | grep -Pom1 '^\d+'
}

JOB=$(queue)
if [[ -n $JOB ]]; then
  atrm $JOB 2>/dev/null
  log "remove queued job $JOB"
else
  log "no queued job present"
fi

echo "/usr/local/emhttp/webGui/scripts/reload_services ${1:-1}" | at -M now 2>/dev/null
log "queue new job $(queue), wait for ${1:-1}s"
exit 0
